package com.ruoyi.system.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.system.domain.FamilyMedicine;
import com.ruoyi.system.domain.MedicationRecord;
import com.ruoyi.system.service.IFamilyMedicineService;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Medicine;
import com.ruoyi.system.service.IMedicineService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 药品信息Controller
 *
 * @author shan
 * @date 2025-04-13
 */
@RestController
@RequestMapping("/system/medicine")
public class MedicineController extends BaseController {
    @Autowired
    private IMedicineService medicineService;

    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private IFamilyMedicineService iFamilyMedicineService;

    /**
     * 查询药品信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:medicine:list')")
    @GetMapping("/list")
    public TableDataInfo list(Medicine medicine) {
        startPage();
        List<Medicine> list = medicineService.selectMedicineList(medicine);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:medicine:list')")
    @GetMapping("/listFamilyMedicine")
    public TableDataInfo list(FamilyMedicine medicine) {
        startPage();
        List<FamilyMedicine> list = iFamilyMedicineService.selectMedicineList(medicine);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:medicine:list')")
    @GetMapping("/listUseMedicine")
    public AjaxResult listUseMedicine(MedicationRecord medicine) {
        startPage();
        List<MedicationRecord> list= medicineService.listUseMedicine(medicine);
        return AjaxResult.success(list);
    }


    @PreAuthorize("@ss.hasPermi('system:medicine:list')")
    @GetMapping("/listNull")
    public AjaxResult listNull(Medicine medicine) {
        List<Medicine> list = medicineService.selectMedicineList(null);
        return AjaxResult.success(list);
    }


    /**
     * 导出药品信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:medicine:export')")
    @Log(title = "药品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Medicine medicine) {
        List<Medicine> list = medicineService.selectMedicineList(medicine);
        ExcelUtil<Medicine> util = new ExcelUtil<Medicine>(Medicine.class);
        util.exportExcel(response, list, "药品信息数据");
    }

    /**
     * 获取药品信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:medicine:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(medicineService.selectMedicineById(id));
    }

    /**
     * 新增药品信息
     */
    @PreAuthorize("@ss.hasPermi('system:medicine:add')")
    @Log(title = "药品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Medicine medicine) {
        medicine.setCreateBy(getUserId().toString());
        medicine.setUpdateBy(getUserId().toString());
        int i = medicineService.insertMedicine(medicine);
        List<Medicine> medicines = medicineService.selectMedicineList(medicine);

        // 插入到字典表中
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictSort(0L);
        sysDictData.setDictLabel(medicine.getMedicineName());
        sysDictData.setDictValue(medicines.get(0).getId().toString());
        sysDictData.setDictType("yaopin");
        sysDictData.setListClass("default");
        sysDictData.setIsDefault("N");
        sysDictData.setStatus("0");
        sysDictData.setCreateBy(getUserId().toString());
        sysDictData.setCreateTime(new Date());
        dictDataService.insertDictData(sysDictData);

        return toAjax(i);
    }

    /**
     * 修改药品信息
     */
    @PreAuthorize("@ss.hasPermi('system:medicine:edit')")
    @Log(title = "药品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Medicine medicine) {
        medicine.setUpdateBy(getUserId().toString());
        return toAjax(medicineService.updateMedicine(medicine));
    }

    /**
     * 删除药品信息
     */
    @PreAuthorize("@ss.hasPermi('system:medicine:remove')")
    @Log(title = "药品信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(medicineService.deleteMedicineByIds(ids));
    }

    /**
     * 新增药品信息
     */
    @PreAuthorize("@ss.hasPermi('system:medicine:add')")
    @Log(title = "药品信息", businessType = BusinessType.INSERT)
    @PostMapping("/addFamilyMedicine")
    public AjaxResult addFamilyMedicine(@RequestBody FamilyMedicine medicine) {
        medicine.setCreateBy(getUserId().toString());
        medicine.setUpdateBy(getUserId().toString());
        int i = iFamilyMedicineService.insertFamilyMedicine(medicine);
        return toAjax(i);
    }

    //新增用药提醒

    @PreAuthorize("@ss.hasPermi('system:medicine:add')")
    @Log(title = "用药提醒", businessType = BusinessType.INSERT)
    @PostMapping("/addMedRecord")
    public AjaxResult addMedRecord(@RequestBody MedicationRecord medicine) {
        medicine.setCreateBy(getUserId().toString());
        medicine.setUpdateBy(getUserId().toString());
        int i = iFamilyMedicineService.addMedRecord(medicine);
        return toAjax(i);
    }

}
